<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="招募阶段" prop="stage">
        <el-select
          v-model="queryParams.stage"
          placeholder="请选择招募阶段"
          clearable
        >
          <el-option
            v-for="dict in dict.type.recruitment_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="治疗阶段" prop="treatmentStage">
        <el-select
          v-model="queryParams.treatmentStage"
          placeholder="请选择治疗阶段"
          clearable
        >
          <el-option
            v-for="dict in dict.type.treatment_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="药品名称" prop="drug">
        <el-input
          v-model="queryParams.drug"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登记号" prop="registrationNum">
        <el-input
          v-model="queryParams.registrationNum"
          placeholder="请输入登记号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ydz:clinical:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ydz:clinical:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ydz:clinical:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ydz:clinical:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="clinicalList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="招募阶段" align="center" prop="stage">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.recruitment_stage"
            :value="scope.row.stage"
          />
        </template>
      </el-table-column>

      <el-table-column label="试验分期" align="center" prop="trialStaging">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.trial_staging"
            :value="
              scope.row.trialStaging ? scope.row.trialStaging.split(',') : []
            "
          />
        </template>
      </el-table-column>
      <el-table-column label="治疗阶段" align="center" prop="treatmentStage">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.treatment_stage"
            :value="scope.row.treatmentStage"
          />
        </template>
      </el-table-column>

      <el-table-column label="药品名称" align="center" prop="drug" />
      <el-table-column label="登记号" align="center" prop="registrationNum" />
      <el-table-column label="创建人名称" align="center" prop="createName" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ydz:clinical:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ydz:clinical:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改临床招募对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="60%"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品疾病" prop="clinicalDiseaseList">
              <el-select
                v-model="form.clinicalDiseaseList"
                placeholder="请药品疾病"
                value-key="diseaseId"
                filterable
                multiple
                @remove-tag="handleRemoveTag"
              >
                <el-option
                  v-for="item in diseaseList"
                  :key="item.value"
                  :label="item.name"
                  :value="{ diseaseId: item.id }"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="适应症" prop="indications">
              <el-input
                type="textarea"
                v-model="form.indications"
                placeholder="请输入适应症"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="招募阶段" prop="stage">
              <el-radio-group v-model="form.stage">
                <el-radio
                  v-for="dict in dict.type.recruitment_stage"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否推荐" prop="state">
              <el-radio-group v-model="form.state">
                <el-radio
                  v-for="dict in dict.type.is_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sexType">
              <el-select v-model="form.sexType" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sex_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="试验分期" prop="trialStaging">
              <el-checkbox-group v-model="form.trialStaging">
                <el-checkbox
                  v-for="dict in dict.type.trial_staging"
                  :key="dict.value"
                  :label="dict.value"
                >
                  {{ dict.label }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="治疗阶段" prop="treatmentStage">
              <el-select
                v-model="form.treatmentStage"
                placeholder="请选择治疗阶段"
              >
                <el-option
                  v-for="dict in dict.type.treatment_stage"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="地区" prop="trialArea">
          <el-checkbox-group v-model="form.trialArea">
            <el-checkbox
              v-for="dict in dict.type.provinces_code"
              :key="dict.value"
              :label="dict.value"
            >
              {{ dict.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="药品名称" prop="drug">
              <el-input v-model="form.drug" placeholder="请输入药品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" placeholder="请输入年龄" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                clearable
                v-model="form.startTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择开始时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker
                clearable
                v-model="form.endTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择结束时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="登记号" prop="registrationNum">
              <el-input
                v-model="form.registrationNum"
                placeholder="请输入登记号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人数" prop="peopleNum">
              <el-input v-model="form.peopleNum" placeholder="请输入人数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="项目介绍">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="入选条件">
          <editor v-model="form.selected" :min-height="192" />
        </el-form-item>
        <el-form-item label="排除条件">
          <editor v-model="form.exclusion" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listClinical,
  getClinical,
  delClinical,
  addClinical,
  updateClinical,
} from "@/api/clinical";
import { listDisease } from "@/api/disease";

export default {
  name: "Clinical",
  dicts: [
    "provinces_code",
    "sex_type",
    "trial_staging",
    "recruitment_stage",
    "treatment_stage",
    "is_yes_no",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 临床招募表格数据
      clinicalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        indications: null,
        stage: null,
        sexType: null,
        trialStaging: null,
        trialName: null,
        treatmentStage: null,
        trialArea: null,
        areaName: null,
        drug: null,
        registrationNum: null,
        peopleNum: null,
      },
      // 表单参数
      form: {
        clinicalDiseaseList: null,
      },
      diseaseList: null,
      //疾病删除数组
      delClinicalDiseaseIds: [],
      //回显时疾病数组，用来删除比对
      clinicalDis: [],
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.listDisease();
  },
  methods: {
    /** 查询临床招募列表 */
    getList() {
      this.loading = true;
      listClinical(this.queryParams).then((response) => {
        this.clinicalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    listDisease() {
      let params = {
        pageNum: 1,
        pageSize: 10000,
      };
      listDisease(params).then((response) => {
        this.diseaseList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleRemoveTag(value) {
      this.clinicalDis.forEach((element) => {
        if (element.diseaseId == value.diseaseId) {
          this.delClinicalDiseaseIds.push(element.id);
          if (!this.delClinicalDiseaseIds.includes(element.id)) {
            this.delClinicalDiseaseIds.push(element.id);
          }
          return;
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        indications: null,
        state: null,
        stage: null,
        sexType: null,
        trialStaging: [],
        trialName: null,
        treatmentStage: null,
        trialArea: [],
        areaName: null,
        drug: null,
        age: null,
        startTime: null,
        endTime: null,
        registrationNum: null,
        peopleNum: null,
        content: null,
        selected: null,
        exclusion: null,
        createBy: null,
        createName: null,
        createTime: null,
        updateBy: null,
        updateName: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加临床招募";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getClinical(id).then((response) => {
        this.form = response.data;
        this.form.trialStaging = this.form.trialStaging.split(",");
        this.form.trialArea = this.form.trialArea.split(",");
        this.form.clinicalDiseaseList = response.data.clinicalDiseaseList;
        this.clinicalDis = response.data.clinicalDiseaseList;
        this.delClinicalDiseaseIds = [];
        this.open = true;
        this.title = "修改临床招募";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.delClinicalDiseaseIds = this.delClinicalDiseaseIds;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.trialStaging = this.form.trialStaging.join(",");
          this.form.trialArea = this.form.trialArea.join(",");
          if (this.form.id != null) {
            updateClinical(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClinical(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除临床招募编号为"' + ids + '"的数据项？')
        .then(function () {
          return delClinical(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ydz/clinical/export",
        {
          ...this.queryParams,
        },
        `clinical_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
::v-deep .el-select{
  width: 100%;
}
::v-deep .el-date-editor{
  width: 100%;
}
</style>